package com.cheyun.netsalev3.view.dm.detail

import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.RecyclerView
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.adapter.MyAdapter
import com.cheyun.netsalev3.databinding.ActivityDmCallRecordBinding
import com.cheyun.netsalev3.utils.BaseActivity
import com.cheyun.netsalev3.utils.InjectorUtils
import com.cheyun.netsalev3.utils.api.NetworkState
import com.cheyun.netsalev3.viewmodel.DmCallRecordActivityViewModel

class DmCallRecordActivity : BaseActivity<ActivityDmCallRecordBinding>() {
    override fun initContentView(): Int {
        return R.layout.activity_dm_call_record
    }
    var id = 0
    override fun initViewModel(): ViewModel? {
        id = intent.getIntExtra("id",0)
        val factory = InjectorUtils.getDmCallRecordModelFactory(id)
        return ViewModelProviders.of(this, factory).get(DmCallRecordActivityViewModel::class.java)
    }

    private lateinit var adapter: MyAdapter

    override fun initView() {
        adapter = MyAdapter(R.layout.dm_call_record_item, binding.viewModel)
        var recyclerView = binding.root.findViewById<RecyclerView>(R.id.swipe_target)
        recyclerView.adapter = adapter
        binding.swipeRefreshLayout.setOnRefreshListener {
            binding.viewModel?.refresh()
        }
    }

    override fun initData() {
        binding.viewModel?.data?.observe(this, Observer { list ->
            adapter.submitList(list)
        })
        binding.viewModel?.networkState?.observe(this, Observer {
            binding.swipeRefreshLayout.isRefreshing = it == NetworkState.LOADING
        })
    }

}
